# Clean Environment -----------------------------------------------------
rm(list = ls())

# Load Data ---------------------------------------------------------------

covariates <- read_rds("df_full_uod.rds")
imce <- read_rds("IMCEs.rds")
source("figure_design.R")

imce %>% left_join(covariates, by = "ResponseId") -> df_imce_full

# Table D.1 ---------------------------------------------------------------

# Note that results were computed running under Windows 11 x64 (build 22631) -- estimates vary slightly in the second or third digits when run on Mac despite specifying seed 

m1 <- lm_robust(judges_M_boot ~ lib + maj + auth + party_preference + income + education + financial_situation + gender + age, data = df_imce_full)
m2 <- lm_robust(judges_A_boot ~ lib  + maj + auth + party_preference + income + education + financial_situation + gender + age, data = df_imce_full)

m3 <- lm_robust(media_M_boot ~ lib  + maj + auth + party_preference + income + education + financial_situation + gender + age, data = df_imce_full)
m4 <- lm_robust(media_A_boot ~ lib  + maj + auth + party_preference + income + education + financial_situation + gender + age, data = df_imce_full)

texreg(list(m1,m2,m3,m4),
       file = "tab_d1.tex",
       include.ci = F,
       digits = 3,
       custom.header = list("Judges" = 1:2, "Media" = 3:4),
       custom.model.names = c("Majoritarian","Authoritarian","Majoritarian","Authoritarian"),
       custom.coef.map = list(
         "lib" = "Liberal understanding",
         "maj" = "Majoritarian understanding",
         "auth" = "Authoritarian understanding",
         "party_preferenceCivic Coalition" = "Civic Coalition",
         "party_preferencePoland 2050" = "Poland 2050",
         "party_preferenceConfederation" = "Confederation",
         "party_preferenceThe Left" = "The Left",
         "party_preferenceDon't know/none" = "Don't know/other",
         "income2001-3000 zł" = "2001-3000 zł",
         "income3001-4000 zł" = "3001-4000 zł",
         "income4001-6000 zł" = "4001-6000 zł",
         "income>6000 zł" = "$>$6000 zł",
         "incomeNo response" = "No response",
         "educationSecondary/Post-secondary" = "Secondary/Post-secondary",
         "educationHigher" = "Higher",
         "educationNo response" = "No response ",
         "financial_situationFair" = "Fair",
         "financial_situationGood/very good" = "Good/very good",
         "genderMale" = "Male (vs. female)",
         "age"="Age"),
       booktabs = T,
       include.rmse = FALSE,
       groups = list("Party preference (baseline = PiS)" = 4:8,
                     "Income (baseline = $<$2000 zł)" = 9:13,
                     "Education (baseline = Primary or vocational)" = 14:16,
                     "Financial situation (baseline = Poor/very poor)" = 17:18))

# Figure D.1 --------------------------------------------------------------

# Note that results were computed running under Windows 11 x64 (build 22631) -- parameter vary slightly when run on Mac despite specifying seed 

load("cjbart_rating.RData")

vimp_estimates_rating[["results"]]  %>%  
  mutate(covar = dplyr::recode(covar, "party_preference" = "Party preference",
                               "lib" = "Liberal understanding",
                               "maj" = "Majoritarian understanding",
                               "auth" = "Authoritarian understanding",
                               "age" = "Age",
                               "education" = "Education",
                               "income" = "Income",
                               "financial_situation" = "Financial situation",
                               "choice" = "Choice order",
                               "gender" = "Gender"),
         covar = factor(covar, levels = c("Choice order","Age","Gender","Financial situation", "Education","Income","Party preference","Authoritarian understanding",
                                          "Majoritarian understanding","Liberal understanding")),
         Level = dplyr::recode(Level, "judges_M" = "Judges: majoritarian",
                              "judges_A" = "Judges: authoritarian",
                              "media_M" = "Media: majoritarian",
                              "media_A" = "Media: authoritarian"),
         Level = factor(Level, levels = c("Judges: majoritarian","Judges: authoritarian","Media: majoritarian","Media: authoritarian"))) -> 
  estimates_rating

ggplot(estimates_rating, aes(x = importance, y = covar)) +
  geom_vline(xintercept = 0, linetype = "dashed", color = "red") +
  geom_point(size = 2) +
  geom_errorbarh(aes(xmin = lower2.5, xmax = upper97.5), height = 0.5) +
  facet_wrap(~ Level, scales = "free", ncol = 2) +
  labs(x = "Standarized importance parameter",
       y = "Covariate") ->
  figure_cjbart

ggsave(figure_cjbart, file = "fig_d1.pdf",width = 10, height = 6)

# Figure D.2 --------------------------------------------------------------

# Note that results were computed running under Windows 11 x64 (build 22631) -- parameter vary slightly when run on Mac despite specifying seed 

load("cjbart_choice.RData")

vimp_estimates[["results"]]  %>%  
  mutate(covar = dplyr::recode(covar, "party_preference" = "Party preference",
                               "lib" = "Liberal understanding",
                               "maj" = "Majoritarian understanding",
                               "auth" = "Authoritarian understanding",
                               "age" = "Age",
                               "education" = "Education",
                               "income" = "Income",
                               "financial_situation" = "Financial situation",
                               "choice" = "Choice order",
                               "gender" = "Gender"),
         covar = factor(covar, levels = c("Choice order","Age","Gender","Financial situation", "Education","Income","Party preference","Authoritarian understanding",
                                          "Majoritarian understanding","Liberal understanding")),
         Level = dplyr::recode(Level, "judges_M" = "Judges: majoritarian",
                               "judges_A" = "Judges: authoritarian",
                               "media_M" = "Media: majoritarian",
                               "media_A" = "Media: authoritarian"),
         Level = factor(Level, levels = c("Judges: majoritarian","Judges: authoritarian","Media: majoritarian","Media: authoritarian"))) -> 
  estimates_rating

ggplot(estimates_rating, aes(x = importance, y = covar)) +
  geom_vline(xintercept = 0, linetype = "dashed", color = "red") +
  geom_point(size = 2) +
  geom_errorbarh(aes(xmin = lower2.5, xmax = upper97.5), height = 0.5) +
  facet_wrap(~ Level, scales = "free",ncol = 2) +
  labs(x = "Standarized importance parameter",
       y = "Covariate") ->
  figure_cjbart

ggsave(figure_cjbart, file = "fig_d2.pdf",width = 10, height = 6)
